﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Loogn.OrmLite;
using Stardust.ConfigCenterWeb.Code.Entities;
using Stardust.Common.Enum;
using System.Text;

namespace Stardust.ConfigCenterWeb.Code.Repositories.Impl
{
    public class ServerNodeRepository : IServerNodeRepository
    {

        public static ServerNodeRepository Instance = new ServerNodeRepository();
        public long Add(ServerNode m)
        {
            using (var db = DB.Open())
            {
                var id = db.Insert(m, true);
                return id;
            }
        }

        public long Update(ServerNode m)
        {
            using (var db = DB.Open())
            {

                var flag = db.Update<ServerNode>(m);
                return flag;
            }
        }


        public long Delete(long nodeId)
        {
            using (var db = DB.Open())
            {
                var flag = db.DeleteById<ServerNode>(nodeId);
                return flag;
            }
        }

        public int Heartbeat(long nodeId)
        {
            using (var db = DB.Open())
            {
                var flag = db.UpdateFieldById<ServerNode>("HeartbeatTime", DateTime.Now, nodeId);
                if (flag > 0) //写这里不好，应该是service中
                {
                    db.Update<ServerNode>(DictBuilder.Assign("Status", ServerNodeStatus.Normal), "ID=@id and Status=@status",
                        DictBuilder.Assign("id", nodeId).Assign("status", ServerNodeStatus.Disconnect));
                }
                return flag;
            }
        }

        public int Logout(long nodeId)
        {
            using (var db = DB.Open())
            {
                var flag = db.UpdateFieldById<ServerNode>("Status", ServerNodeStatus.Disconnect, nodeId);
                return flag;
            }
        }


        public ServerNode GetNodeByName(string serviceName, string address)
        {
            using (var db = DB.Open())
            {
                var node = db.SingleWhere<ServerNode>(DictBuilder
                    .Assign("ServiceName", serviceName)
                    .Assign("Address", address));
                return node;
            }
        }

        public List<ServerNode> SearchList(string serviceName, string address, string version, ServerNodeStatus? status)
        {
            using (var db = DB.Open())
            {
                var conditions = DictBuilder.New();
                if (!string.IsNullOrEmpty(serviceName))
                {
                    conditions.Add("ServiceName", serviceName);
                }
                if (!string.IsNullOrEmpty(address))
                {
                    conditions.Add("Address", address);
                }
                if (!string.IsNullOrEmpty(version))
                {
                    conditions.Add("Version", version);
                }
                if (status != null && status.Value != 0)
                {
                    conditions.Add("Status", status.Value);
                }

                var list = db.SelectWhere<ServerNode>(conditions);
                return list;
            }
        }


        public List<ServerNode> GetCheckAliveNodes()
        {
            using (var db = DB.Open())
            {
                var list = db.SelectFmt<ServerNode>("SELECT * FROM ServerNode where HeartbeatTime<'{0}' and Status=1", DateTime.Now.AddSeconds(-8).ToString("yyyy-MM-dd HH:mm:ss"));
                return list;
            }
        }

        public List<ServerNode> GetCheckRegisterNodes()
        {
            using (var db = DB.Open())
            {
                var list = db.SelectFmt<ServerNode>("SELECT * FROM ServerNode where HeartbeatTime<'{0}' and Status=2", DateTime.Now.AddSeconds(-15).ToString("yyyy-MM-dd HH:mm:ss"));
                return list;
            }
        }


    }
}